<?php

namespace app\api\controller;

use think\Db;
use app\common\controller\Api;

/****
 * Class    Goods
 * @name    商品API文件
 * @author  晓月老板<1434926265@qq.com>
 * @date    2020/05/20 11:22 AM
 * @package app\api\controller
 */
class Goods extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    /***
     * @name    获取首页商品列表<按照后台权重排序>
     * @author  晓月老板<1434926265@qq.com>
     * @date    2020/05/20 11:17 AM
     * @return  string<json>
     */
    public function goods_list()
    {

        header("Access-Control-Allow-Origin:*");

        // 页数
        $offset = $this->request->get("offset", 0);

        // 获取分类ID
        $category_id = $this->request->request("category_id",0,"intval");

        // 排序
        $sort = $this->request->request("sort",null,"trim");

        // 服务id
        $service_id = $this->request->request("service_id",null,"trim");

        // 商品数量
        $limit = 20;

        $where = array();

        $where['status'] = 'up';

        // 销量排序 hot
        if(isset($sort)){

            $order = $sort.' desc';

            $where['store_id'] = ['>','0'];

        }else{

            $order = 'weigh desc';

        }

        // 服务标签筛选
        if(isset($service_id)){

            $where[] = ['exp',Db::raw("FIND_IN_SET($service_id,service_ids)")];

        }

        if($category_id > 0){

            $data['list'] = Db::name('shopro_goods')->where($where)->where('find_in_set('.$category_id.',category_ids)')->order($order)->limit($offset, $limit)->select();

        }else{

            // 查询商品数据
            $data['list'] = Db::name('shopro_goods')->where($where)->order($order)->limit($offset, $limit)->select();

        }

        // 商品数量
        $data['total'] = Db::name('shopro_goods')->where(array('status'=>'up'))->count();

        $this->success('请求成功', $data);

    }

    /***
     * @name    商品详情API接口
     * @author  晓月老板<1434926265@qq.com>
     * @date    2020/05/20 09:38 AM
     * @return  string<json>
     */
    public function detail()
    {

        // 获取商品ID
        $goods_id = $this->request->request("goods_id",0,"intval");

        // 查询商品数据
        $goods_info = Db::name('shopro_goods')->where(array('id'=>$goods_id,'status'=>'up'))->find();

        // sku规格
        $skuList = \app\admin\model\shopro\GoodsSku::all(['pid' => 0, 'goods_id' => $goods_id]);
        if ($skuList) {
            foreach ($skuList as &$s) {
                $s->children = \app\admin\model\shopro\GoodsSku::all(['pid' => $s->id, 'goods_id' => $goods_id]);
            }
        }

        // sku规格价格
        $skuPrice = \app\admin\model\shopro\GoodsSkuPrice::all(['goods_id' => $goods_id]);

        $data['goods_info'] = $this->format_goods($goods_info);

        $data['skuList'] = $skuList;

        $data['skuPrice'] = $skuPrice;

        // 商品浏览记录

        $this->success('请求成功'.$this->auth->id, $data);

    }




    /***
     * @name    商品评价
     * @author  晓月老板<1434926265@qq.com>
     * @date    2020/05/22 15:03 PM
     * @return  string<json>
     */
    public function goods_comment()
    {

        // 页数
        $offset = $this->request->get("offset", 0);

        // 获取商品ID
        $goods_id = $this->request->request("goods_id",0,"intval");

        // 评价数量
        $limit = 5;

        $where = array();

        $where['status'] = 'show';//状态可显示

        $where['goods_id'] = $goods_id;//当前商品

        // 查询商品评价列表
        $data['list'] = Db::name('shopro_goods_comment')->where($where)->limit($offset, $limit)->select();

        // 商品数量
        $data['total'] = Db::name('shopro_goods_comment')->where($where)->count();

        $this->success('请求成功', $data);

    }

    // 格式化商品基本信息
    public function format_goods($goods)
    {

        // 处理商品轮播图
        if($goods['images']){

            $goods['images'] = explode(',',$goods['images']);

        }

        // 处理商品属性
        $goods['params'] = json_decode($goods['params'],true);

        return $goods;

    }


    // 获取商品服务标签
    public function goods_service()
    {

        // 获取店铺ID
        $store_id = $this->request->request("store_id", 0, "intval");

        $data['list'] = Db::name('shopro_goods_service')->where(array('store_id'=>$store_id))->select();

        $this->success('请求成功', $data);

    }

}
